package com.hltx.controller.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.hltx.controller.base.BaseController;
import com.hltx.pojo.common.Page;
import com.hltx.service.goods.VipCoodsManager;
import com.hltx.service.index.IndexManager;
import com.hltx.util.CustomUtil;
import com.hltx.util.ResultInfo;

/**
 * vip商城controller
 * @author gzw
 *
 */
@Controller
@RequestMapping("/vip")
public class VipGoodsController extends BaseController{
	
	@Autowired
	private VipCoodsManager vipGoodsService;
	@Resource(name="indexService")
	private IndexManager indexService;
	/**
	 * 打开vip商品列表
	 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request,HttpSession session) {
		String parentid=request.getParameter("parentid");
		String search = CustomUtil.strTransCode(request.getParameter("search"));
		String classifyname =  CustomUtil.strTransCode(request.getParameter("classifyname"));
		if(parentid == null){parentid = "goods";}
		if(!(search == null || "".equals(search))){parentid = "search";}			
		ModelAndView view  = this.getModelAndView();
		view.addObject("parentid", parentid);
		view.addObject("search", search);
		view.addObject("classifyname", classifyname);
		view.setViewName("vip/vipgoods_list");
		return view;
	}
	@RequestMapping(value = "/getVipGoodsLists", method = { RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> getVipGoodsLists(Page p, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询vip商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> goodsList = vipGoodsService.getVipGoodsLists(p);
			resMap.put("rGoodsList", goodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/getHotvipGoodsList", method = { RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> getHotvipGoodsList(Page p, HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询热门商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			List<Map<String, Object>> hotGoodsList = vipGoodsService.getHotvipGoodsList(p);
			resMap.put("rHotGoodsList", hotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/searchvipGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  searchvipGoodsListByParmas(Page page){
		this.logBefore(logger, "按条件查询商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> rGoodsList = vipGoodsService.searchvipGoodsListByParmas(page);
			resMap.put("rGoodsList", rGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/searchvipHotGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  searchvipHotGoodsListByParmas(Page page){
		this.logBefore(logger, "按条件查询热门商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> rHotGoodsList = vipGoodsService.searchvipHotGoodsListByParmas(page);
			resMap.put("rHotGoodsList", rHotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/getvipHotGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  getvipHotGoodsListByParmas(Page page, String parentid, String search){
		this.logBefore(logger, "按条件和分类查询热门商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try {
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			params.put("search", search);
			page.setParams(params);
			List<Map<String, Object>> rHotGoodsList = vipGoodsService.getvipHotGoodsListByParmas(page);
			resMap.put("rHotGoodsList", rHotGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/viphotGoodsListByParentId", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  viphotGoodsListByParentId(Page page,String parentid){
		this.logBefore(logger, "显示分类热门");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try {
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			page.setParams(params);
			List<Map<String, Object>> list = vipGoodsService.getvipHotGoodsListByPid(page);
			resMap.put("rHotGoodsList", list);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/getvipGoodsListByParmas", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  getvipGoodsListByParmas(Page page, String parentid, String search, String orders){
		this.logBefore(logger, "按条件和分类查询商品列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try {
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			params.put("search", search);
			params.put("order", orders);
			page.setParams(params);
			List<Map<String, Object>> rGoodsList = vipGoodsService.getvipGoodsListByParmas(page);
			resMap.put("rGoodsList", rGoodsList);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	
	@RequestMapping(value = "/getVipRecommended", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> getRecommended(@RequestParam Map<String, String> params, Page p,HttpServletRequest request, HttpSession session){
		this.logBefore(logger, "查询推荐列表");
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			
			List<Map<String, Object>> rVipHeadList = vipGoodsService.getVipRecommended(params);
			/*List<Map<String, Object>> rVipGoodsList = vipGoodsService.getVipGoodsList(p);*/
			resMap.put("rVipHeadList", rVipHeadList);
		    /*	resMap.put("rVipGoodsList", rVipGoodsList);*/
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
	@RequestMapping(value = "/vipgoodsListByParentId", method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object>  vipgoodsListByParentId(Page page,String parentid,String orders){
		this.logBefore(logger, "显示分类商品");
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> params =new HashMap<>();
		try { 
			String classIds = indexService.getClassIdByPid(parentid);
			params.put("classIds", classIds);
			params.put("order", orders);
			page.setParams(params);
			List<Map<String, Object>> list = vipGoodsService.getvipGoodsListByPid(page);
			resMap.put("rGoodsList", list);
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resMap;
	}
}
